/**
 * @filename:StkotheroutController Feb 5, 2025
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;

import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.domain.Stkotherout;
import com.wonder.app.service.StkotheroutService;
import com.wonder.app.request.StkotheroutRequest;
import com.wonder.framework.authorization.CheckAdminToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * <p>说明： 其他出库单API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    Feb 5, 2025
 *
 */
@Api(tags = "其他出库单")
@RestController
@RequestMapping("/app/Stkotherout")
public class StkotheroutController {
	
	@Autowired
	private StkotheroutService stkotheroutService;

	@Autowired
	private SysAdminService adminService;

	@Autowired
	private CheckAdminToken checkAdminToken;
	
	@ApiOperation(value = "其他出库单列表")
	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public Map<String,Object> index(@RequestBody StkotheroutRequest stkotheroutRequest) {
		Page<Stkotherout> page = new Page<Stkotherout>(stkotheroutRequest.getPage(), stkotheroutRequest.getLimit());
		IPage<Stkotherout> list = stkotheroutService.selectList(stkotheroutRequest, page);
		
		Map<String,Object> retMap=new HashMap<>();
		retMap.put("data",CommonPage.restPage(list));
		
  
		return AjaxMap.success(retMap);
	}
	
	/**
	 * @explain 修改排序开关
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/updateExt")
	@ApiOperation(value = "修改排序开关", notes = "作者：Wonder")
	public AjaxResult<Stkotherout> updateExt(@RequestBody Stkotherout entity){
		if (null!=entity) {
			boolean rsg = stkotheroutService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 添加
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/add")
	@ApiOperation(value = "添加", notes = "作者：Wonder")
	public AjaxResult<Stkotherout> add(@RequestBody Stkotherout entity){

		if (null!=entity) {
		    
		    String validate=stkotheroutService.validate(entity,"new");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);    
			boolean rsg = stkotheroutService.save(entity);
			if (rsg) {
				  return AjaxResult.success("system.insertSuccess");
			  }else {
				  return AjaxResult.failed("system.insertFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 查询对象  <swagger GET请求>
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/getUpdateInfo")
	@ApiOperation(value = "获取其他出库单对象", notes = "作者：Wonder")
	public AjaxResult<Stkotherout> getUpdateInfo(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");
		Long Id=Long.parseLong(map.get("Id").toString());		
		 Stkotherout stkotherout=stkotheroutService.getById(Id);
		
		if (null!=stkotherout ) {
			 return AjaxResult.success(stkotherout);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}
	
	/**
	 * @explain 修改
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "作者：Wonder")
	public AjaxResult<Stkotherout> update(@RequestBody Stkotherout entity){
		if (null!=entity) {
		    String validate=stkotheroutService.validate(entity,"update");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);
			boolean rsg = stkotheroutService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 删除
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除", notes = "作者：Wonder")
	public AjaxResult<Stkotherout> delete(@RequestBody Map<String,Object> map){
		if(map==null)return AjaxResult.failed("system.parameterError");
		String ids=map.get("Id").toString();
		ids=StringUtils.remove(ids, '[');
		ids=StringUtils.remove(ids, ']');
		String[] deleteIds=StringUtils.split(ids, ',');
		List<Long> idList=new ArrayList<>(); 
		for(int i=0;i<deleteIds.length;i++)
		{
			idList.add( Long.parseLong(deleteIds[i].trim()));//整数数组
		}

	    boolean rsg = stkotheroutService.removeBatchByIds(idList);
	    if (rsg) {
		    return AjaxResult.success("system.deleteSuccess");
	    }else {
		   return AjaxResult.failed("system.deleteFailed");
	    }
		
	}
	/**
	 * @explain 查看子项明细
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@ApiOperation(value = "查询stkotherout明细")
	@ResponseBody
	@PostMapping(value = "/stkotheroutdetail")

	public AjaxResult<Map<String,Object>> getStkotheroutDetail(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
		if (null == map){
			return AjaxResult.failed("system.parameterError");
		}
		Map<String,Object> data = stkotheroutService.getStkotheroutDetail(map);
		if (null != data){
			return AjaxResult.success(data);
		} else {
			return AjaxResult.failed();
		}
	}

	/**
	 * @explain 查看子项明细(手机端)
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@ApiOperation(value = "其他出库单明细(手机端)")
	@ResponseBody
	@PostMapping(value = "/get_other_out_detail")

	public AjaxResult<Map<String,Object>> getOtheroutDetail(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
		if (null == map){
			return AjaxResult.failed("system.parameterError");
		}
		Map<String,Object> data = stkotheroutService.getOtheroutDetail(map);
		if (null != data){
			return AjaxResult.success(data);
		} else {
			return AjaxResult.failed();
		}
	}

	@ApiOperation(value = "其他差出库单列表(手机端)")
	@RequestMapping(value = "/get_other_out_list", method = RequestMethod.POST)
	public AjaxResult<Map<String,Object>> getOtherOutList(@RequestBody Map<String,Object> map) {
		Map<String,Object> data = stkotheroutService.getOtherOutList(map);
		if (null != data){
			return AjaxResult.success(data);
		} else {
			return AjaxResult.failed();
		}
	}


	@ApiOperation(value = "其他出库单审核")
	@RequestMapping(value = "/audit_other_out", method = RequestMethod.POST)
	public AjaxResult<?> auditOtherIn(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		String userName = user.getName();
		Long user_id = user.getUser_id();

		boolean res = stkotheroutService.auditOtherOut(user_id,userName,map);
		if (res){
			return AjaxResult.success(res);
		} else {
			return AjaxResult.failed();
		}
	}

	/**
	 * 根据物料编码查询库存信息
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/get_inventory_by_material")
	@ApiOperation(value = "根据物料编码查询库存信息", notes = "作者：Wonder")
	public AjaxResult<?> getInventoryByMaterial(HttpServletRequest request,@RequestBody Map<String,Object> map)throws Exception{
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		String userName = user.getName();
		Long user_id = user.getUser_id();
		Map<String, Object> data = stkotheroutService.getInventoryByMaterial(user_id,map);

		return AjaxResult.success(data);

	}
}